local StrategyHelper = {}
local AttributeConst = require("const.AttributeConst")

function StrategyHelper.simpleHurt(atkAttr, defAttr, ratio)
    local value = math.floor(atkAttr:getAttrValue(AttributeConst.ATTACK) * ratio)
    local pierce = atkAttr:getAttrValue(AttributeConst.PHYSICS_PIERCE)
    local defense = defAttr:getAttrValue(AttributeConst.DEFENSE)
    local hurt = value - math.floor(defense - defense * (pierce / 1000))
    local hurtAdd = atkAttr:getAttrValue(AttributeConst.HURT_ADD)
    hurt = hurt + math.floor(hurt * (hurtAdd / 1000))
    local physics_resist = defAttr:getAttrValue(AttributeConst.PHYSICS_RESIST)
    hurt = hurt - math.floor(hurt * (physics_resist / 1000)) -- 抗性千分比
    hurt = hurt < 0 and 0 or hurt
    return StrategyHelper.critHurt(atkAttr, hurt)
end

function StrategyHelper.elementHurt(atkAttr, defAttr, ratio, element)
    local value = math.floor(atkAttr:getAttrValue(AttributeConst.ATTACK) * ratio)
    local defense = defAttr:getAttrValue(AttributeConst.DEFENSE)
    local hurt = value - math.floor(defense * 0.8)
    local elementHurt = AttributeConst.ELEMENT_HURT_MAP[element]
    local hurtAdd = atkAttr:getAttrValue(AttributeConst.HURT_ADD)
    hurt = hurt + math.floor(hurt * ((elementHurt + hurtAdd) / 1000))
    local resist = AttributeConst.ELEMENT_RESIST_MAP[element]
    assert(resist)
    local resist = defAttr:getAttrValue(resist)
    hurt = hurt - math.floor(hurt * (resist / 1000)) -- 抗性千分比
    hurt = hurt < 0 and 0 or hurt
    return StrategyHelper.critHurt(atkAttr, hurt)
end

function StrategyHelper.critHurt(atkAttr, hurt)
    local result = hurt
    local isCrit = false
    local crit = atkAttr:getAttrValue(AttributeConst.CRIT)
    local crit_hurt = atkAttr:getAttrValue(AttributeConst.CRIT_HURT)
    local pro = math.random(1, 1000)
    if pro <= crit then
        result = result + math.floor(hurt * (0.5 + crit_hurt / 1000))
        isCrit = true
    end
    return result, isCrit
end

function StrategyHelper.counterattackHurt(atkAttr, defAttr)
    local value = atkAttr:getAttrValue(AttributeConst.ATTACK)
    local pierce = atkAttr:getAttrValue(AttributeConst.PHYSICS_PIERCE)
    local defense = defAttr:getAttrValue(AttributeConst.DEFENSE)
    local hurt = value - math.floor(defense - defense * (pierce / 1000))
    local counterattack_hurt = atkAttr:getAttrValue(AttributeConst.COUNTERATTACK_HURT)
    hurt = math.floor(hurt * (0.1 + counterattack_hurt / 1000))
    local physics_resist = defAttr:getAttrValue(AttributeConst.PHYSICS_RESIST)
    hurt = hurt - math.floor(hurt * (physics_resist / 1000)) -- 抗性千分比
    hurt = hurt < 0 and 0 or hurt
    return hurt
end

return StrategyHelper
